package com.wh.wisdomsite.xbox.server.controller.du;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wh.wisdomsite.xbox.common.util.ResManager;
import com.wh.wisdomsite.xbox.du.entity.DustProCheckPoint;
import com.wh.wisdomsite.xbox.du.service.IDustProCheckPointService;
import com.wh.wisdomsite.xbox.pojo.du.DustDevDataIn;
import com.wh.wisdomsite.xbox.pojo.du.DustProCheckPointIn;
import com.wh.wisdomsite.xbox.pr.service.IProjectService;
import com.wh.wisdomsite.xbox.server.util.DateUtils;
import com.wh.wisdomsite.xbox.server.util.UserUtils;
import com.wh.wisdomsite.xbox.server.util.WrapperUtil;
import com.wh.wisdomsite.xbox.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 扬尘设备实时数据
 * @author 刘旭
 * @version V1.0
 * @Package com.wh.wisdomsite.xbox.server.controller.du
 * @date 2018年08月27日 11:48
 * @Copyright 企业版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2018
 */
@Controller
@RequestMapping(value = "/dustDevData")
public class DustDevDataController {
    private static final Log logger = LogFactory.getLog(DustDevDataController.class);

    @Autowired
    private IProjectService iProjectService;

    @Autowired
    private IDustProCheckPointService iDustProCheckPointService;

    /**
     * 跳转到扬尘设备实时数据
     *
     * @return
     */
    @RequiresPermissions("rules:du_dustDevData_page")
    @RequestMapping(value = "/index.do", method = RequestMethod.GET)
    public String index() {
        return "du/dustDevData/dustDevDataList";
    }

    /**
     * 获取扬尘设备实时数据列表
     *
     * @param response
     * @param in
     */
    @RequiresPermissions("rules:du_dustDevData_find")
    @RequestMapping(value = "/dustDevDataList.do")
    public void dustDevDataList(HttpServletResponse response, DustDevDataIn in) {
        try {
            logger.info("加载设备对应的项目数据列表-->>开始");
            if (in.getPage() == 0) in.setPage(1);
            if (in.getRows() == 0) in.setRows(10);
            Page<DustProCheckPoint> page = new Page<DustProCheckPoint>();
            page.setCurrent(in.getPage());
            page.setSize(in.getRows());
            //设置条件
            EntityWrapper<DustProCheckPoint> ewDustPointPrj = new EntityWrapper<DustProCheckPoint>();
            ewDustPointPrj.setEntity(new DustProCheckPoint());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewDustPointPrj.eq("pr.prId",in.getPrId());
            }
            //若用户不是超级管理员根据用户的数据权限添加查询条件
            if(!UserUtils.isSysAdmin()){
                ewDustPointPrj = UserUtils.setEntityWrapper(ewDustPointPrj, "co", "off", "pr");
            }
            ewDustPointPrj.eq("d.dataState",1);
            ewDustPointPrj.groupBy("pr.prId");
            Page<Map<String, Object>> projectPage = iDustProCheckPointService.getPrjMapsPage(page,ewDustPointPrj);
            JSONObject json = new JSONObject();
            JSONArray array = new JSONArray();
            if (projectPage.getRecords().size() > 0) {
                for(Map<String, Object> project : projectPage.getRecords()){
                    JSONObject object = new JSONObject();
                    object.put("prId",project.get("prId"));
                    object.put("prName",project.get("prName"));
                    object.put("pbUnitName",project.get("pbUnitName"));
                    object.put("poUnitName",project.get("poUnitName"));
                    object.put("psUnitName",project.get("psUnitName"));
                    array.put(object);
                }
            }
            json.put("total", projectPage.getTotal());
            json.put("rows", array);
            ResManager.getJsonOut(response).print(json);
            logger.info("加载设备对应的项目数据-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 扬尘设备实时数据详情
     * @param prId
     * @param request
     * @return
     */
    @RequestMapping("dustDevDataDesc.do")
    public String dustDevDataDesc(String prId, HttpServletRequest request) {
        logger.info("扬尘设备实时数据详情-->>开始");
        Map<String , Object> dustDevData = iDustProCheckPointService.getPrjMapById(prId);
        request.setAttribute("dustDevData", dustDevData);
        logger.info("扬尘设备实时数据详情-->>结束");
        return "du/dustDevData/dustDevDataDesc";
    }

    /**
     * 获取设备对应的项目数据列表
     * @param request
     * @param in
     */
    @RequestMapping(value = "/toDustDeviceList.do")
    public String toDustDeviceList(HttpServletRequest request, DustDevDataIn in) {
        String projectId = in.getPrId();
        String onLineStatus = in.getOnLineStatus();
        request.setAttribute("projectId",projectId);
        request.setAttribute("onLineStatus",onLineStatus);
        return "du/dustDevData/dustDeviceList";
    }

    /**
     * 根据项目id获取扬尘设备
     * @param in
     */
    @RequiresPermissions("rules:du_dustDevRecord_deviceListBtn")
    @RequestMapping(value = "/dustDeviceList.do")
    public void dustDeviceList(HttpServletResponse response, DustProCheckPointIn in) {
        try {
            logger.info("获取扬尘设备列表-->>开始");
            //设置条件
            WrapperUtil<DustProCheckPoint> ewDustProCheckPoint = new WrapperUtil<DustProCheckPoint>();
            ewDustProCheckPoint.setEntity(new DustProCheckPoint());
            if(StringUtils.isNotEmpty(in.getPrId())){
                ewDustProCheckPoint.eq("prId",in.getPrId());
            }
            if(null != in.getOnLineStatus()){
                ewDustProCheckPoint.in("onLineStatus",String.valueOf(in.getOnLineStatus()));
            }
            ewDustProCheckPoint.eq("dataState",1);
            List<DustProCheckPoint> checkPointList = iDustProCheckPointService.selectList(ewDustProCheckPoint);
            JSONArray array = new JSONArray();
            for(DustProCheckPoint checkPoint : checkPointList){
                JSONObject obj = new JSONObject(checkPoint);
                //在线时间
                String onlineTime = "";
                Integer onlineState = checkPoint.getOnLineStatus();
                if(onlineState == 1){
                    Date date = new Date();
                    Date dateOnline = new Date();
                    if(null != checkPoint.getUpdateDate()){
                        dateOnline = checkPoint.getUpdateDate();
                    }else{
                        dateOnline = checkPoint.getCreateDate();
                    }
                    onlineTime = DateUtils.getDifferenceTime(dateOnline,date);
                }
                obj.put("onlineTime",onlineTime);
                array.put(obj);
            }
            ResManager.getJsonOut(response).print(array);
            logger.info("获取扬尘设备列表-->>结束");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





}
